package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"mytest/code"
	"mytest/database"
	"mytest/flower-order/model"
	"mytest/util"
	"strconv"
	"time"
)

type StatisticsController struct {
}

func (t *StatisticsController) Index(c *gin.Context) {
	var orderList []*model.Order
	var config *model.Config
	err := database.DB.Select("value").Where("name=?", "holiday_type").First(&config).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		c.JSON(code.Success, util.ReturnError(code.SysErr, err.Error()))
		return
	}
	if err != nil && err == gorm.ErrRecordNotFound {
		c.JSON(code.Success, util.ReturnError(code.SysErr, "请在系统设置中配置节日类型"))
		return
	}
	holidayType := config.Value
	beforLastYear := time.Now().Local().Year() - 2
	err = database.DB.
		Select("*,datetime(start_time) as start_time,datetime(end_time) as end_time").
		Where("year > ? and holiday_type = ?", beforLastYear, holidayType).
		Order("id asc").
		Find(&orderList).
		Error
	if err != nil && err != gorm.ErrRecordNotFound {
		c.JSON(code.Success, util.ReturnError(code.SysErr, err.Error()))
		return
	}
	nowTimeUnix := time.Now().Unix()
	statistics := make(map[string]int32)
	//配送类型 0.自取 1.自配 2.达达
	echartsRecords := map[int32][]int32{
		0: {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 0.自取
		1: {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 1.自配
		2: {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 2.达达
		3: {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 3.专车
	}
	//订单类型 0.微信 1.门店 2.美团 3.饿了么 4.抖音 5.高德
	echartsPies := map[int32]int32{
		0: 0, //0.微信
		1: 0, //1.门店
		2: 0, //2.美团
		3: 0, //3.饿了么
		4: 0, //4.抖音
		5: 0, //5.高德
	}
	//订单类型 0.微信 1.门店 2.美团 3.饿了么 4.抖音 5.高德
	echartsDataset := map[int32][]int32{
		0: {0, 0, 0, 0}, //订单状态 0.未处理 1.待配送 2.已完成 3.退款
		1: {0, 0, 0, 0}, //订单状态 0.未处理 1.待配送 2.已完成 3.退款
		2: {0, 0, 0, 0}, //订单状态 0.未处理 1.待配送 2.已完成 3.退款
		3: {0, 0, 0, 0}, //订单状态 0.未处理 1.待配送 2.已完成 3.退款
		4: {0, 0, 0, 0}, //订单状态 0.未处理 1.待配送 2.已完成 3.退款
		5: {0, 0, 0, 0}, //订单状态 0.未处理 1.待配送 2.已完成 3.退款
	}
	//data["echartsMap"] = make(map[string]interface{})
	statistics["total_order"] = int32(len(orderList))
	statistics["pay_amount"] = 0
	statistics["refund_amount"] = 0
	statistics["total_amount"] = 0
	statistics["undeal_order"] = 0
	statistics["undeal_order_timeout"] = 0
	statistics["wait_send_order"] = 0
	statistics["wait_send_order_timeout"] = 0
	statistics["complete_order"] = 0
	statistics["refund_order"] = 0

	for _, v := range orderList {
		orderEndTime, _ := time.ParseInLocation("2006-01-02 15:04:05", v.EndTime, time.Local)
		orderEndTimeUnix := orderEndTime.Unix()
		if v.SendPrice > 0 { //支出
			statistics["pay_amount"] += v.SendPrice
		}
		switch v.OrderStatus { //订单状态 0.未处理 1.待配送 2.已完成 3.退款
		case 0:
			statistics["total_amount"] += v.OrderPrice
			statistics["undeal_order"] += 1     //未处理订单
			if nowTimeUnix > orderEndTimeUnix { //未处理订单-超时
				statistics["undeal_order_timeout"] += 1
			}
			echartsRecords[v.SendType], err = GenerateEchartData(v, echartsRecords[v.SendType])
		case 1:
			statistics["total_amount"] += v.OrderPrice
			statistics["wait_send_order"] += 1  //待配送订单
			if nowTimeUnix > orderEndTimeUnix { //待配送订单-超时
				statistics["wait_send_order_timeout"] += 1
			}
			echartsRecords[v.SendType], err = GenerateEchartData(v, echartsRecords[v.SendType])
		case 2:
			statistics["total_amount"] += v.OrderPrice
			statistics["complete_order"] += 1
		case 3:
			statistics["refund_amount"] += v.OrderPrice
			statistics["refund_order"] += 1
		}
		echartsPies[v.OrderType] += 1 //0.线下 1.美团 2.饿了么 3.抖音 4.高德
		echartsDataset[v.OrderType][v.OrderStatus] += 1
	}
	data := make(map[string]interface{})
	data["statistics"] = statistics
	data["echartsRecords"] = echartsRecords
	data["echartsPies"] = echartsPies
	data["echartsDataset"] = echartsDataset
	c.JSON(code.Success, util.ReturnSuccess(data))
	return
}

var timeMap = map[string]int32{
	"08:00": 0,
	"08:30": 1,
	"09:00": 2,
	"09:30": 3,
	"10:00": 4,
	"10:30": 5,
	"11:00": 6,
	"11:30": 7,
	"12:00": 8,
	"12:30": 9,
	"13:00": 10,
	"13:30": 11,
	"14:00": 12,
	"14:30": 13,
	"15:00": 14,
	"15:30": 15,
	"16:00": 16,
	"16:30": 17,
	"17:00": 18,
	"17:30": 19,
	"18:00": 20,
	"18:30": 21,
	"19:00": 22,
	"19:30": 23,
	"20:00": 24,
	"20:30": 25,
	"21:00": 26,
	"21:30": 27,
	"22:00": 28,
	"22:30": 29,
	"23:00": 30,
	"23:30": 31,
	"24:00": 32,
}

func GenerateEchartData(order *model.Order, echartData []int32) ([]int32, error) {
	startTimeObj, err := time.ParseInLocation("2006-01-02 15:04:05", order.StartTime, time.Local)
	if err != nil {
		fmt.Println(err.Error())
		return echartData, err
	}
	var hour, mint string
	if startTimeObj.Hour() >= 10 {
		hour = strconv.Itoa(startTimeObj.Hour())
	} else {
		hour = fmt.Sprintf("0%v", startTimeObj.Hour())
	}
	if startTimeObj.Minute() >= 30 {
		mint = "30"
	} else {
		mint = "00"
	}
	index := timeMap[hour+":"+mint]
	echartData[index] += 1

	if order.StartTime == order.EndTime {
		return echartData, err
	}

	endTimeObj, err := time.ParseInLocation("2006-01-02 15:04:05", order.EndTime, time.Local)
	if err != nil {
		return echartData, err
	}
	if endTimeObj.Hour() >= 10 {
		hour = strconv.Itoa(endTimeObj.Hour())
	} else {
		hour = fmt.Sprintf("0%v", endTimeObj.Hour())
	}
	if endTimeObj.Minute() >= 30 {
		mint = "30"
	} else {
		mint = "00"
	}
	index2 := timeMap[hour+":"+mint]
	for i := index + 1; i <= index2; i++ {
		echartData[i] += 1
	}
	return echartData, err
}
